properties([
   parameters([
      booleanParam(name: 'FORCE_REBUILD', defaultValue: false, description: 'Force Rebuild'),
      string(name: 'MAX_WORKES_PER_NODE', defaultValue: '1', description: '')
    ])
 ])

def projectName = 'Test'
def originProjectDir = config.projectDir(projectName)

node('master')
{
   stage("Prepare")
   {
      // 挂载Z盘
      bat "if not exist z:\\ ( net use z: ${config.mountDir()} /y )"

      // 创建Log目录
      def logDir = "${originProjectDir}\\${config.distributeLogDir()}"
      bat "if not exist ${logDir} ( mkdir ${logDir} )"

      // 拷贝unity程序
      config.copyUnityDir()

      // 开始前，删除所有可能因上次编译错误或者手动停止的lock
      utils.deleteAllLockFiles(projectName)
   }

   stage('Master: Build Manifest')
   {
      def logDir = "${originProjectDir}\\${config.distributeLogDir()}"
      def logFilePath = "${logDir}\\distribute${config.buildName()}.log"

      def forceRebuild = ""
      if(params.FORCE_REBUILD)
      {
         forceRebuild = " -forceRebuild "
      }

      // 这里一定要用原始工程目录，因为，之后的打包数据：均依赖该路径数据
      bat "${config.localUnityPath()} -quit -batchmode -projectPath ${originProjectDir} ${forceRebuild} -executeMethod AssetBundleDistributer.Run -distributedMaster -logfile ${logFilePath}"
   }

   stage('Master: Distribute Tasks')
   {
      // 读清单文件，以获得要编译的ab包数量（即work数量）
      def mainfestFile = "${originProjectDir}\\distribute.manifest"
      datas = readYaml (file: "${mainfestFile}")

      def workCount = datas.WorkCount

      int paramsWorkesPerNode = params.MAX_WORKES_PER_NODE
      utils.distributeTask(projectName, "test-worker", workCount, paramsWorkesPerNode)
   }
}
